#!/bin/sh
# =============================================================================
# oracle Unified Data Protector
# Copyright (c) 2011 oracle Inc. All Rights Reserved
#
# This program code contains trade secrets of oracle Inc. that
# (i) derive independent economic value, actual or potential, from not being
# generally known to, and not being readily ascertainable by proper means by
# other persons who can obtain economic value from their disclosure or use and;
# (ii) are the subject of efforts by oracle Inc that are reasonable under the
# circumstances to maintain their secrecy. You may not use this program code
# without prior written authorization from oracle Inc.
#
# Description: shell interface with sqlplus
#              takes two parameters 1. login string and 2. query; separated by ;
#
# ==============================================================================

#OLDIFS=${IFS}
#IFS=';'
#read  input
#set -- $input

if [ $# -lt 3 ]; then
   exit 1
fi

query=$1
sid=$2
instid=$3
parameter=$4
parameter2=$5
orauser=`cat /opt/howlink/lb-agent/application.properties | grep "^agent.oracle-user" | awk -F'=' '{ print $2 }' | sed 's/ //g'`
if [ -z "$orauser" ];then
    orauser=`ps -ef | grep ora_pmon | grep -v grep | head -n1 | sed -e 's/^[ \t]*//' | cut -d" " -f1`
fi
#orauser=`ps -ef | grep -v grep | grep ora_pmon_"$instid$" | sed -e 's/^[ \t]*//' | cut -d" " -f1`

#start================. /howlink/hl_scripts/commresource.sh=========================
os=`uname -s`
otab=/etc/oratab

awkc=awk
if [ $os = "SunOS" ]; then
   otab=/var/opt/oracle/oratab
   awkc=nawk
fi

#if [ $sid = "dummydbwithhome" ]; then
#    oraclehome=$orahome
#else
oraclehome=`cat /opt/howlink/lb-agent/application.properties | grep "^agent.oracle-home" | awk -F'=' '{ print $2 }' | sed 's/ //g'`
if [ -z "$oraclehome" ];then
    oraclehome=`grep -wi "$sid:"  $otab | grep "^$sid" | head -n1 | cut -f2 -d":"`
fi
#fi

TNSDIR=''
#if [ -z "$nlistener" ]; then
TNSDIR=`ps -ef | grep tns | grep -v grep | grep -i listener | $awkc NR==1 | $awkc -F"/bin" '{print $(NF-1)}' | awk '{print $NF}'`
#else
#   TNSDIR=`ps -ef | grep tns | grep -v grep | grep -i $nlistener | $awkc NR==1 | $awkc -F"/bin" '{print $(NF-1)}' | awk '{print $NF}'`
#   if [ ! -d "$TNSDIR" ]; then
#      TNSDIR=`ps -ef | grep tns | grep -v grep | grep -i -w $nlistener | $awkc NR==1 |  $awkc -F"/bin" '{print $(NF-1)}' | awk '{print $NF}'`
#   fi
#fi

TNS_ADMIN=$TNSDIR/network/admin
#tnsdir="/act/act_scripts/TNSADMIN_"$sid".tns"
#if [ -f $tnsdir ]; then
#   TNS_ADMIN=`cat $tnsdir`
#fi

PATH=$oraclehome/bin:$PATH
ORACLE_HOME=$oraclehome
ORACLE_SID=$instid
LD_LIBRARY_PATH=$oraclehome/lib${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}
export PATH
export ORACLE_SID
export ORACLE_HOME
export TNS_ADMIN
export LD_LIBRARY_PATH
#================. /howlink/hl_scripts/commresource.sh=========================end

#check orauser is valid
if [ ! -z "$orauser" ]; then
result=`id $orauser`
if [ $? -ne 0 ]; then
   uid=$orauser
   orauser=`$awkc -v val=$uid -F ":" '$3==val{print $1}' /etc/passwd`
   result=`id $orauser`
   if [ $? -ne 0 ]; then
      orauser=''
   fi
fi
fi

export PATH
export ORACLE_SID
export ORACLE_HOME
export TNS_ADMIN
export LD_LIBRARY_PATH

set +x
sqlplus=$oraclehome/bin/sqlplus
#if [ ! "$authtype" = "os" ]; then
#psph=`/opt/act/bin/udsagent decode -d $sid dp`
#login=`echo $login | sed "s/xxx/${psph}/g"`
#retval=`(cat - << EOF ; cat - ) | $sqlplus -s -L /nolog | grep -vi connected
#connect $login
#set pages 0
#set head off
#set feed off
#set verify off
#set timing off
#set termout on
#$query;
#exit;
#EOF`
#else
if [ `echo $query |awk -F" " '{print $2}'` ];then
	if [ -f `echo $query |awk -F" " '{print $1}'` ];then
		grp=`id -gn $orauser`
		par=`echo $query |awk -F" " '{print $2}'`
		quy=`echo $query |awk -F" " '{print $1}'`
		if [ -z "$grp" ]; then
      			 echo "failed to get group for $user"
       			 return 1
    		fi
    		chown $orauser:$grp $quy
    		chmod 755 $quy
		query="$quy $par"
	else
		echo "error"
		exit 1;
	fi
else
	if [ -f "$query" ]; then
			grp=`id -gn $orauser`
    			if [ -z "$grp" ]; then
       				echo "failed to get group for $user"
       				return 1
    			fi
    			chown $orauser:$grp $query
    			chmod 755 $query
	else
		exit 1;
	fi
fi
#os auth, need to check orauser
if [ -z "$orauser" ]; then
   echo "Could not determine os user for $sid"
   exit 1
fi
#check orauser

if [ -n $parameter ];then
	query="$query $parameter"
fi
if [ -n $parameter2 ];then
    query="$query $parameter2"
fi
if [ "$os" != "Linux" ]; then
 retval=`su $orauser -c "LD_LIBRARY_PATH=$ORACLE_HOME/lib;export LD_LIBRARY_PATH;$sqlplus -s -L / as sysdba| grep -vi connected" << EOF
set pages 0
set head off
set feed off
set verify off
set timing off
set termout on
@$query;
exit;
EOF`
else
 retval=`su -m $orauser -c "LD_LIBRARY_PATH=$ORACLE_HOME/lib;export LD_LIBRARY_PATH;$sqlplus -s -L / as sysdba | grep -vi connected" << EOF
set pages 0
set head off
set feed off
set verify off
set timing off
set termout on
@$query;
exit;
EOF`
fi
#fi
#IFS=$OLDIFS
export status=$retval
echo $retval